#include "vlcplayer.h"

#include <QVBoxLayout>
//#include <phonon/VideoCategory>


VlcPlayer::VlcPlayer(QDeclarativeItem *parent) :
    QDeclarativeItem(parent)
{

    phPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory);
    phPlayer->autoFillBackground();
     phPlayer->createWinId();
    proxy = new QGraphicsProxyWidget(this);
    proxy->setWidget(phPlayer);
    proxy->setPos(0,0);
    //phPlayer->setGeometry(0,0,300,300);
    connect(this,SIGNAL(widthChanged()),this,SLOT(refreshGeometry()));
    connect(this,SIGNAL(heightChanged()),this,SLOT(refreshGeometry()));



    /*Nepomuk::ResourceManager::instance()->init();
    Nepomuk::Resource f("/home/manu/image107.jpg");
    qDebug()<<"exists:"<< f.exists();*/
    //Nepomuk::Tag tag( "MyTag42" );
    //tag.setLabel("myLabel");
    //tag.setProperty();
    //f.addTag( tag );
    //Nepomuk::File f2("/home/manu/La\ Légende\ De\ Bruce\ lee\ 2011.avi");
    /*QList<Nepomuk::Tag> tags = f.tags();
    QListIterator<Nepomuk::Tag> it( tags );
    while( it.hasNext() ){
        Nepomuk::Tag t = it.next();
        qDebug() << "File tagged with tag: " << t.genericLabel() << " ==>"<< t.label() ;
    }*/

/*
    vlcvideo = new QWidget();
    QGraphicsProxyWidget * proxy = new QGraphicsProxyWidget(this);
    proxy->setWidget(vlcvideo);
    vlcvideo->setGeometry(0,0,300,300);
    inst = libvlc_new(0, NULL);
*/
    //QVBoxLayout* mainLayout = new QVBoxLayout(vlcvideo);
    //mainLayout->setMargin(0);
    //mainLayout->addWidget(vlcvideo);
    //vlcvideo->setAttribute(Qt::WA_NativeWindow, true);
    //vlcvideo->setAttribute(Qt::WA_DontCreateNativeAncestors, true);
    //vlcvideo = new QWidget();



    //vlcvideo->setParent(parent);
    //this->setWidget(vlcvideo);
    /*
    QGraphicsProxyWidget * proxy = new QGraphicsProxyWidget(this);
    proxy->setWidget(vlcvideo);
    inst = libvlc_new(0, NULL);
    */
}

void VlcPlayer::refreshGeometry (){
    proxy->setGeometry(this->childrenRect());
}


void VlcPlayer::setMedia(const QString media){
    m_name=media;
}

QString VlcPlayer::media() const{
    return m_name;
}

void VlcPlayer::start() {
    phPlayer->play(QUrl(m_name));
}

void VlcPlayer::startVcl(){


    m = libvlc_media_new_path (inst, m_name.toStdString().c_str());

    /* Create a media player playing environement */
    libvlc_media_player_t *  mp = libvlc_media_player_new_from_media (m);

    /* No need to keep the media now */
    libvlc_media_release (m);


    //Ievietojam vlc QWidget rāmi + ar pārbaudi kas tev par sistemu
#if defined(Q_WS_X11)
    libvlc_media_player_set_xwindow(mp, vlcvideo->winId());
#elif defined(Q_WS_MAC)
    libvlc_media_player_set_nsobject(mp, vlcvideo->winId());
#elif defined(Q_WS_WIN)
    libvlc_media_player_set_hwnd(mp, vlcvideo->winId());
#else
#error "unsupported platform"
#endif

    /* play the media_player */
    libvlc_media_player_play (mp);

}
